/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.bancodedados.daoimplemente.backup;

import camada.bancodedados.conexao.Conexao;
import eloma.classe.backup.Backup;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author KLINGER
 */
public class BackupDAOImpl {

    private static Connection con;

    //@Override
    public void atualizarBackup(Backup backup) {
        PreparedStatement pstm = null;
        try {
            final String SQL_UPDATE
                    = "UPDATE BACKUP SET path = ? , user = ?, password = ?,"
                    + "banco = ?, caminho = ? "
                    + "WHERE idBackup = ?";
            pstm = (PreparedStatement) Conexao.getCon().prepareStatement(SQL_UPDATE);
            pstm.setString(1, backup.getPath());
            pstm.setString(2, backup.getUser());
            pstm.setString(3, backup.getPassword());
            pstm.setString(4, backup.getBanco());
            pstm.setString(5, backup.getCaminho());
            pstm.setInt(6, backup.getIdBackup());
            pstm.executeUpdate();

        } catch (Exception e) {

        }

    }

    //@Override
    public Backup pesquisaBackup() throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            final String SQL_PESQUISA = "SELECT * FROM BACKUP";
            stmt = Conexao.getCon().prepareStatement(SQL_PESQUISA);
            rs = stmt.executeQuery();
            return carregarResultadoSimples(rs);
        } catch (SQLException sqle) {
            throw new Exception(sqle);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
    }

    private Backup carregarResultadoSimples(ResultSet rs) throws SQLException {
        if (rs.next()) {
            Backup dto = new Backup();
            carregarVO(dto, rs);
            return dto;
        } else {
            return null;
        }
    }

    private void carregarVO(Backup dto, ResultSet rs) throws SQLException {
        dto.setIdBackup(rs.getInt("idBackup"));
        dto.setPath(rs.getString("path"));
        dto.setBanco(rs.getString("banco"));
        dto.setUser(rs.getString("user"));
        dto.setPassword(rs.getString("password"));
        dto.setCaminho(rs.getString("caminho"));
    }

}
